<?php

/**
 * Created by PhpStorm.
 * User: DELL
 * Date: 1/19/15
 * Time: 10:48 AM
 */
class GetStaplesTechCommand extends CConsoleCommand {
    public function run($args) {
        $root_url = "http://www.staples.com/Clearance-Deals-Technology/cat_CL211635";
        $dom = new simple_html_dom();
        $dom->load(HSCommon::curl($root_url));
        if ($dom == null) return;

        $page_wrapper = $dom->find('div[class="perpage"]', 0);
        if ($page_wrapper == null) {
            $limit_page = 1;
        } else {
            $arr_li = $page_wrapper->find('li');
            if (count($arr_li) >= 1) {
                end($arr_li);
                $key = key($arr_li);
                $li = $arr_li[$key - 1];
                $li_dom = new simple_html_dom();
                $li_dom->load($li);
                $limit_page = trim($li_dom->plaintext);
            } else {
                $limit_page = 1;
            }
        }

        for ($i = $limit_page; $i >= 1; $i--) {
            $url = $root_url . "?pagenum=$i";
            $dom = new simple_html_dom();
            $dom->load(HSCommon::curl($url));

            if ($dom == null) continue;
            $ul = $dom->find('ul[id="productDetail"]', 0);
            if ($ul == null) continue;
            $li = $ul->find('li');

            foreach ($li as $item) {
                //handle price
                $price_wrapper = $item->find('i[class="price"]', 0);
                if ($price_wrapper == null) continue;
                $price = trim(str_replace('*', '', $price_wrapper->plaintext));

                $pwas = $item->find('dd[class="pwas"]', 0);
                if ($pwas != null) {
                    $old_price = $pwas->find('del', 0);
                    if ($old_price != null) {
                        $old_price = trim($old_price->plaintext);
                        $price = "<span>$price </span> <span><del>$old_price</del></span>";
                    }
                }

                //handle name
                $name_wrapper = $item->find('div[class="name"]', 0);
                if ($name_wrapper == null) continue;
                $name_wrapper = $name_wrapper->find('a', 0);
                if ($name_wrapper == null) continue;
                $name = trim($name_wrapper->plaintext);

                $link = preg_match('/www.staples.com/', $name_wrapper->href) ? $name_wrapper->href : "http://www.staples.com" . $name_wrapper->href;

                //handle image
                $img = $item->find('img[class="photo"]', 0);

                $deal = new Deal();
                $deal->name = $name;
                $deal->url = $link;
                $deal->price = $price;
                $deal->picture = $img == null ? "" : $img->src;
                $deal->created_date = date('d-m-Y');
                $deal->type = DealTypeEnum::staples;

                $old_deal = Deal::model()->findByAttributes(array(
                    'name' => $deal->name,
//                    'created_date' => $deal->created_date,
                    'type' => $deal->type
                ));

                if ($old_deal != null) continue;

                $deal->save();
            }
        }
    }
}